package com.youi.service.impl;

import com.youi.common.DelStatus;
import com.youi.dao.MarketThemeAdvertisementDao;
import com.youi.entity.MarketAdvertisement;
import com.youi.entity.Operationlog;
import com.youi.entity.Sysuser;
import com.youi.service.MarketThemeAdvertisementService;
import com.youi.service.OperationlogService;
import com.youi.util.CommonUtil;
import com.youi.util.DateUtil;
import com.youi.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class MarketThemeAdvertisementServiceImpl extends BaseServiceImpl<MarketAdvertisement> implements MarketThemeAdvertisementService {

    private MarketThemeAdvertisementDao marketThemeAdvertisementDao;
    @Autowired
    private OperationlogService operationlogService;

    @Autowired
    public void setfaultRecordDao(MarketThemeAdvertisementDao marketThemeAdvertisementDao) {
        this.marketThemeAdvertisementDao = marketThemeAdvertisementDao;
        super.setBaseDAO(marketThemeAdvertisementDao);
    }

    @Override
    public void addOrEditAdvertisement(Long id, MarketAdvertisement marketAdvertisement, Sysuser sysuser, String ip, String name, String imgUrl, String url, Integer position, Date gmtBegin, Date gmtEnd) {

        //进行基础表的编辑
        if (id != null) { //修改

        } else { //新增，进行初始化
            marketAdvertisement = new MarketAdvertisement();
            marketAdvertisement.setDelStatus(DelStatus.NODEL);
            marketAdvertisement.setGmtCreated(new Date());
            marketAdvertisement.setStatus(0);
            marketAdvertisement.setPosition(0);
            marketAdvertisement.setCreator(sysuser.getId());
            Integer count = marketThemeAdvertisementDao.getCount(null);
            marketAdvertisement.setCode(String.valueOf(1001 + count));
        }
        if (!url.equals("")) {
            Map<String, Object> urlParams = CommonUtil.parseUrl(url);
            if (urlParams.get("id") != null) {
                marketAdvertisement.setToThemeId(Long.valueOf(String.valueOf(urlParams.get("id"))));
            } else {
                marketAdvertisement.setToThemeId(Long.valueOf(-1));
            }
        }

        marketAdvertisement.setImgUrl(imgUrl);
        marketAdvertisement.setUrl(url);
        marketAdvertisement.setPosition(position);
        marketAdvertisement.setGmtModify(new Date());
        marketAdvertisement.setName(name);
        marketAdvertisement.setGmtBegin(gmtBegin);
        marketAdvertisement.setGmtEnd(gmtEnd);
        marketThemeAdvertisementDao.saveOrUpdate(marketAdvertisement);

        //操作记录

        if (id == null) {
            operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "添加了广告：" + marketAdvertisement.getName()));
        } else {
            operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "修改了广告：" + marketAdvertisement.getName()));
        }
    }

    @Override
    public void updateAdvertisementEndedStatus() {
        Map<String, Object> sParams = new HashMap();
        sParams.put("status", Arrays.asList(0, 1, 2));
        Map<String, Object> ltGmt = new HashMap();
        ltGmt.put("gmtEnd", DateUtil.simpdfyMdHms.format(new Date()));
        List<MarketAdvertisement> endingThemes = marketThemeAdvertisementDao.getByParamsList(sParams, null, null, null, null, null, ltGmt);
        if (endingThemes.size() > 0) {
            for (MarketAdvertisement marketAdvertisement : endingThemes) {
                marketAdvertisement.setStatus(3);
            }
            marketThemeAdvertisementDao.saveOrUpdateAll(endingThemes);
        }
    }
}
